class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        """
        :type gas: List[int]
        :type cost: List[int]
        :rtype: int
        """
        length = len(gas)

        def traveralClock():
            lowestPoint = 0
            lowestGas = 0
            size = 0
            for i in range(length):
                size += gas[i]
                size -= cost[i]
                if size<lowestGas:
                    lowestGas = size
                    lowestPoint = (i+1) % length

            size = 0
            for i in range(length):
                index = (lowestPoint+i)%length
                size += gas[index]
                size -= cost[index]
                if size<0: return -1

            return lowestPoint
        
        def traveralAntiClock():
            lowestPoint = 0
            lowestGas = 0
            size = 0
            for i in range(length):
                size += gas[-i]
                size -= cost[(-i-1+length)%length]
                if size<lowestGas:
                    lowestGas=size
                    lowestPoint = (-i-1+length)%length
            
            size = 0
            for i in range(length):
                index = (lowestPoint-i)%length
                size += gas[index]
                size -= cost[(index-1+length)%length]
                if size<0: return -1
            return lowestPoint
        
        station = traveralClock()
        if station!=-1: return station
        station = traveralAntiClock()
        return station

print(Solution().canCompleteCircuit([4],[5]))